package com.hyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hyz.model.IsComplete;
import com.hyz.model.ResponseResult;
import com.hyz.model.assetSecondment.AssetSecondment;
import com.hyz.model.assetSecondment.AssetSecondmentResult;
import com.hyz.model.assetSecondment.AssetSecondmentStatus;
import com.hyz.service.AssetSecondmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 */
@Api("部门审批人员")
@RestController
@RequestMapping("/approve")
public class ApproveController {
    @Autowired
    private AssetSecondmentService asService;

    //----------------------------------------------------------------
    // 借调功能

    @ApiOperation("部门审批人员获取资产借用申请")
    @PostMapping("/apGetAssetSecondments")
    public ResponseResult<List<AssetSecondment>> apGetAssetSecondments(@RequestBody List<Long> userIds) {
        QueryWrapper<AssetSecondment> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(AssetSecondment::getApplicantId, userIds)
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .eq(AssetSecondment::getResult, AssetSecondmentResult.CREATE)
                .eq(AssetSecondment::getStatus, AssetSecondmentStatus.BORROW);
        List<AssetSecondment> list = asService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "该部门没有任何借用申请");
        }
        return new ResponseResult<>(200, "成功获取用户的借用申请", list);
    }

    @ApiOperation("部门审批人员对资产借用审批")
    @PostMapping("/apUpdateAssetSecondment")
    public ResponseResult<String> apUpdateAssetSecondment(@RequestParam("assetSecondmentId") Long assetSecondmentId,
                                                          @RequestParam("result") AssetSecondmentResult result) {
        // 1. 判断对应的申请是否存在
        AssetSecondment assetSecondment = asService.getById(assetSecondmentId);
        if (Objects.isNull(assetSecondment)) {
            return new ResponseResult<>(400, "对应申请不存在");
        }
        // 2. 判断对应申请是否已完成
        // 3. 判断对应的申请类型是否为借用
        // 4. 判断对应的申请结果为 0
        if (assetSecondment.getIsComplete() != IsComplete.UNFINISHED ||
                assetSecondment.getStatus() != AssetSecondmentStatus.BORROW ||
                assetSecondment.getResult() != AssetSecondmentResult.CREATE) {
            return new ResponseResult<>(400, "申请已完成，请刷新");
        }

        // 5. 判断传输过来的请求是否符合规范
        if (result.equals(AssetSecondmentResult.FAILED) || result.equals(AssetSecondmentResult.APPROVAL_AGREE)) {
            IsComplete is = IsComplete.UNFINISHED;
            if (Objects.equals(result, AssetSecondmentResult.FAILED)) {
                is = IsComplete.COMPLETE;
            }
            // 6. 对申请结果进行修改
            UpdateWrapper<AssetSecondment> wrapper = new UpdateWrapper<>();
            wrapper.lambda().eq(AssetSecondment::getStatus, AssetSecondmentStatus.BORROW)
                    .eq(AssetSecondment::getResult, AssetSecondmentResult.CREATE)
                    .eq(AssetSecondment::getIsComplete,IsComplete.UNFINISHED)
                    .eq(AssetSecondment::getId, assetSecondmentId)
                    .set(AssetSecondment::getIsComplete, is)
                    .set(AssetSecondment::getResult, result);
            if (asService.update(wrapper)) {
                // 7. 返回所有本部门的资产借用申请
                return new ResponseResult<>(200, "审批成功");
            }
        }
        return new ResponseResult<>(400, "审批错误，请重新选择");
    }


}